export default {
	install(Vue, options) {
        /**
         * 发布动态
         */
        let opts = {
            maxPic: 9,
            maxSize: 5 * 1024 * 1024
        }
        Vue.prototype.$publish = (callback) => {
            wx.showActionSheet({
                itemList: ['从相册选择', '拍摄'],
                success: (res) => {
                    let type = res.tapIndex === 0 ? 'album' : 'camera'
                    wx.chooseImage({
                        sizeType: ['original', 'compressed'],
                        sourceType: [type],
                        count: opts.maxPic,
                        success: (res) => {
                            if (res.errMsg === 'chooseImage:ok') {
                                let pics = []
                                for (let i = 0, len = res.tempFiles.length; i < len; i++) {
                                    let item = res.tempFiles[i]
                                    let {path, size} =  item
                                    // 过滤gif图片
                                    if (path.indexOf('.gif') != -1 || path.indexOf('.GIF') != -1) {
                                        wx.showToast({
                                            title: '不支持gif格式的图片',
                                            icon: 'none'
                                        })
                                        break
                                    }
                                    if (size > opts.maxSize) {
                                        wx.showToast({
                                            title: '上传图片不能超过5M',
                                            icon: 'none'
                                        })
                                        break
                                    }
                                    pics.push({
                                        thumb: path,
                                        url: path,
                                        load: true
                                    })
                                }
                                callback && callback(pics)
                            }
                        }
                    })
                }
            })
        }
	}
}